package bsc.war.handler;

import bsc.ejb.entity.Map;
import bsc.ejb.entity.Person;
import bsc.ejb.entity.Project;
import bsc.ejb.entity.SRM;
import bsc.ejb.entity.SRMTopic;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.entity.ScoreDomain;
import bsc.ejb.entity.ScoreView;
import bsc.ejb.entity.Topic;
import bsc.ejb.entity.TopicScoreView;
import bsc.ejb.entity.TopicScoreView.Type;
import bsc.ejb.metadata.Colorimetry;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.model.Score;
import bsc.war.service.ScoreCardUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author castrosalazar
 */
public class SRMFinalUser extends AbstractController {

    public SRMFinalUser() {
    }

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            Long idSRM = request.getParameter("srm") != null && NumberUtils.isNumber(request.getParameter("srm")) ? Long.parseLong(request.getParameter("srm")) : null;
            ScoreDomain currentSD = (ScoreDomain)request.getSession().getAttribute("currentScoreDomain");
            ScoreView currentView = currentSD.getScoreView();
            HttpSession session = request.getSession(true);
            Person person = (Person) session.getAttribute("person");
            /*
            List<Map> mapList = delegate.findMap();
            List<Map> maps2remove = new LinkedList<Map>();
            for(Map tmp : mapList)
                if( !tmp.getScoreview().equals(currentView) )
                    maps2remove.add(tmp);
            for(Map tmp : maps2remove)
                mapList.remove(tmp);
            request.setAttribute("mapList", mapList);
             */

            List<Map> mapList = new ArrayList<Map>();
            mapList.add((Map)currentView.getScoreCard());
            request.setAttribute("mapList", mapList);

            List<SRM> srmList = delegate.findSRM();
            List<SRM> srms2remove = new LinkedList<SRM>();
            for(SRM tmp : srmList)
                if(!tmp.getScoreView().equals(currentView))
                    srms2remove.add(tmp);
            for(SRM tmp : srms2remove)
                srmList.remove(tmp);
            request.setAttribute("srmList",srmList);

            List<Project> projectList = delegate.findProject(person);
            request.setAttribute("projectList", projectList);

            List<ScoreView> scoreViewList = new ArrayList<ScoreView>(); //delegate.findScoreView();
            scoreViewList.add(currentView);
            request.setAttribute("scoreViewList", scoreViewList);

            if(idSRM == null)return new ModelAndView(new RedirectView("/BSC/mainFinalUser.do"));

            SRM srm = delegate.findSRM(idSRM);
            //double estimatedTime = 0D;
            List<SRMTopic> srmTopics = delegate.findSRMTopic(srm);

            java.util.Map<ScoreCard, String> color = new HashMap<ScoreCard, String>();
            java.util.Map<ScoreCard, Double> scores = new HashMap<ScoreCard, Double>();
            java.util.Map<SRMTopic, Type> statuses = new HashMap<SRMTopic, Type>();

            for (SRMTopic srmTopic : srmTopics) {
                //estimatedTime += topic.getExpectedTime();
                if (srmTopic.getTopic().getScoreCard() != null) {
                    ScoreCard tmpScoreCard = srmTopic.getTopic().getScoreCard();
                    Score score = ScoreCardUtils.calculateScore(delegate, ScoreCardUtils.getSystemComparation(request), tmpScoreCard, ScoreCardUtils.getSystemDate(request));
                    Colorimetry colorimetry = delegate.findColorimetry(tmpScoreCard, score.getScore());
                    scores.put(tmpScoreCard, score.getScore());
                    color.put(tmpScoreCard, colorimetry.getColor());
                }
                Topic topic = srmTopic.getTopic();
                ScoreView scoreView = srm.getScoreView();
                TopicScoreView topicScoreView = delegate.findTopicScoreView(topic, scoreView);

                if(topicScoreView !=null ) statuses.put(srmTopic, topicScoreView.getType() );
            }

            //System.out.println();
            //srm.setExpectedTime(Double.parseDouble((int)estimatedTime/60 +"."+ (int)estimatedTime%60));

            request.setAttribute("color", color);
            request.setAttribute("scores", scores);
            request.setAttribute("statuses", statuses);

            /*
                from SRMTopic u can get:
                Topic topic
                SRM SRM
                private Double expectedTime;
                private String agreement;
                private String responsable;
                private String description;
                private String deal;
             */

            request.setAttribute("srmTopics",srmTopics);
            request.setAttribute("srm",srm);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Caught exception: ", e);
        }
        return new ModelAndView("SRMFinalUser");
    }
}